package com.company.dcm.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.company.dcm.common.R_Receive;
import com.company.dcm.entity.*;
import com.company.dcm.receive.*;
import com.company.dcm.service.*;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

@Slf4j
@RestController
@RequestMapping("/api")
public class ReceiveController {
    @Autowired
    private R_ClothSheetService r_clothSheetService;
    @Autowired
    private R_ClothSheetDetailService r_clothSheetDetailService;
    @Autowired
    private R_DeviceInfoService r_deviceInfoService;
    @Autowired
    private R_DevStopAlarmService r_devStopAlarmService;
    @Autowired
    private R_DevStopHisService r_devStopHisService;
    @Autowired
    private R_FaultService rFaultService;
    @Autowired
    private R_ClothSheetDetailFourScoreService rClothSheetDetailFourScoreService;
    @Autowired
    private R_ClothSheetFourScoreService rClothSheetFourScoreService;

    @Value("${dcm.image-path}")
    private String basePath;

    @PostMapping("/RT_DeviceInfo")
    public R_Receive setRT_DeviceInfo(@RequestBody RT_DeviceInfo rt_deviceInfo){
        R_DeviceInfo rDeviceInfo = new R_DeviceInfo();
        BeanUtils.copyProperties(rt_deviceInfo,rDeviceInfo);
        if(!r_deviceInfoService.updateOrInsertDeviceInfo(rDeviceInfo)){
            log.warn("error {}",rt_deviceInfo);
            return R_Receive.error();
        }
        return R_Receive.success();
    }

    @PostMapping("/RF_ClothSheet/Add")
    public R_Receive addRF_ClothSheet(@RequestBody RF_ClothSheet rf_clothSheet){
        R_ClothSheet rClothSheet = new R_ClothSheet();
        BeanUtils.copyProperties(rf_clothSheet,rClothSheet);
        if(!r_clothSheetService.save(rClothSheet)){
            log.info("error /RF_ClothSheet/Add {}",rf_clothSheet);
            return R_Receive.error();
        }
        return R_Receive.success();
    }

    @PostMapping("/RF_ClothSheet/Edit")
    public R_Receive editRF_ClothSheet(@RequestBody RF_ClothSheet rf_clothSheet){
        R_ClothSheet rClothSheet = new R_ClothSheet();
        BeanUtils.copyProperties(rf_clothSheet,rClothSheet);
        if(r_clothSheetService.getBaseMapper().update(rClothSheet,new LambdaUpdateWrapper<R_ClothSheet>()
                .eq(R_ClothSheet::getSerialNo,rClothSheet.getSerialNo()))<1){
            if(!r_clothSheetService.save(rClothSheet)){
                log.warn("error /RF_ClothSheet/Edit {}",rf_clothSheet);
                return R_Receive.error();
            }
        }
        return R_Receive.success();
    }

    @PostMapping("/RF_ClothSheetDetail/Add")
    public R_Receive addRF_ClothSheetDetail(@RequestBody RF_ClothSheetDetail rf_clothSheetDetail) throws InvocationTargetException, IllegalAccessException {
        R_ClothSheetDetail r_clothSheetDetail = new R_ClothSheetDetail();
        BeanUtils.copyProperties(rf_clothSheetDetail,r_clothSheetDetail);
        if(!r_clothSheetDetailService.save(r_clothSheetDetail)){
            log.warn("error /RF_ClothSheetDetail/Add {}",rf_clothSheetDetail);
            return R_Receive.error();
        }
        return R_Receive.success();
    }

    @PostMapping("/RF_ClothSheetDetailPic/Add")
    public R_Receive addRF_ClothSheetDetailPic(MultipartFile file,
                                               @RequestHeader("KeyPath") String keyPath) throws IOException {
        if (ObjectUtils.isEmpty(file) || file.getSize() <= 0) {
            log.warn("文件为空！");
            return R_Receive.error();
        }

        String rootPath = basePath + keyPath.replaceAll("\\\\","/");
        //原始文件名
        String originalFilename = file.getOriginalFilename();//abc.jpg

        //创建一个目录对象
        File dir = new File(rootPath);
        //判断当前目录是否存在
        if (!dir.exists()) {
            //目录不存在，需要创建
            dir.mkdirs();
        }

       try {
            //将临时文件转存到指定位置
            file.transferTo(new File(rootPath +"/"+ originalFilename));
        } catch (IOException e) {
            e.printStackTrace();
            return R_Receive.error();
        }
        return R_Receive.success();
    }

    @PostMapping("/RF_ClothSheetDetailPic/Add/WithDeviceCode")
    public R_Receive addRF_ClothSheetDetailPicWithoutPath(MultipartFile file,
                                                          @RequestHeader("DeviceCode") String deviceCode) throws IOException {
        if (ObjectUtils.isEmpty(file) || file.getSize() <= 0) {
            log.warn("文件为空！");
            return R_Receive.error();
        }

        String rootPath = basePath +"/"+LocalDate.now()+"/"+ deviceCode +"/";

        //原始文件名
        String originalFilename = file.getOriginalFilename();//abc.jpg

        //创建一个目录对象
        File dir = new File(rootPath);
        //判断当前目录是否存在
        if (!dir.exists()) {
            //目录不存在，需要创建
            dir.mkdirs();
        }

        try {
            //将临时文件转存到指定位置
            file.transferTo(new File(rootPath +"/"+ originalFilename));
        } catch (IOException e) {
            e.printStackTrace();
            return R_Receive.error();
        }
        return R_Receive.success();
    }

    @PostMapping("/RF_DevStopHis/Add")
    public R_Receive addRF_DevStopHis(@RequestBody RF_DevStopHis rf_DevStopHis){
        R_DevStopHis r_DevStopHis = new R_DevStopHis();
        BeanUtils.copyProperties(rf_DevStopHis,r_DevStopHis);
        if(!r_devStopHisService.save(r_DevStopHis)){
            log.warn("error /RF_DevStopHis/Add {}",r_DevStopHis);
            return R_Receive.error();
        }
        return R_Receive.success();
    }

    @PostMapping("/RT_DevStopAlarm/Add")
    public R_Receive addRT_DevStopAlarm(@RequestBody RT_DevStopAlarm rt_DevStopAlarm){
        R_DevStopAlarm r_DevStopAlarm = new R_DevStopAlarm();
        BeanUtils.copyProperties(rt_DevStopAlarm,r_DevStopAlarm);
        if(!r_devStopAlarmService.save(r_DevStopAlarm)){
            log.warn("error /RT_DevStopAlarm/Add {}",r_DevStopAlarm);
            return R_Receive.error();
        }
        return R_Receive.success();
    }

    @PostMapping("/RT_Fault")
    public R_Receive addRT_Fault(@RequestBody RT_Fault rt_fault){
        R_Fault r_fault = new R_Fault();
        BeanUtils.copyProperties(rt_fault,r_fault);
        if(!rFaultService.save(r_fault)){
            log.warn("error /RT_Fault {}",r_fault);
            return R_Receive.error();
        }
        return R_Receive.success();
    }

    @PostMapping("/RF_ClothSheetFourScore/Add")
    public R_Receive addRF_ClothSheetFourScore(@RequestBody RF_ClothSheetFourScore rf_clothSheetFourScore){
        R_ClothSheetFourScore r_clothSheetFourScore = new R_ClothSheetFourScore();
        BeanUtils.copyProperties(rf_clothSheetFourScore,r_clothSheetFourScore);
        if(!rClothSheetFourScoreService.save(r_clothSheetFourScore)){
            log.warn("error /RT_Fault {}",r_clothSheetFourScore);
            return R_Receive.error();
        }
        return R_Receive.success();
    }

    @PostMapping("/RF_ClothSheetDetailFourScore/Add")
    public R_Receive addRF_ClothSheetDetailFourScore(@RequestBody RF_ClothSheetDetailFourScore rf_clothSheetDetailFourScore){
        R_ClothSheetDetailFourScore r_clothSheetDetailFourScore = new R_ClothSheetDetailFourScore();
        BeanUtils.copyProperties(rf_clothSheetDetailFourScore,r_clothSheetDetailFourScore);
        if(!rClothSheetDetailFourScoreService.save(r_clothSheetDetailFourScore)){
            log.warn("error /RT_Fault {}",r_clothSheetDetailFourScore);
            return R_Receive.error();
        }
        return R_Receive.success();
    }
}
